package com.jf3q.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.common.domain.EduMember;
import com.jf3q.common.exception.JfException;
import com.jf3q.common.mapper.EduMemberMapper;
import com.jf3q.common.utils.SaltPassUtils;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.dto.MemberRegDto;
import com.jf3q.front.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Base64;

@Service
public class MemberServiceImpl extends ServiceImpl<EduMemberMapper, EduMember> implements MemberService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public void reg(MemberRegDto registerVo) {
        //获取注册的数据
        String imgCode = registerVo.getImgCode(); //图片验证码
        String imgKey = registerVo.getImgKey(); //图片验证码
        String smsCode = registerVo.getSmsCode(); //短信验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空判断
        if(StringUtils.isEmpty(imgKey) ||StringUtils.isEmpty(imgCode) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(smsCode) || StringUtils.isEmpty(nickname)) {
            throw new JfException(20001,"注册失败-必填项没填写完整");
        }

        //判断手机号是否已经被注册了
        long count = this.count(new LambdaQueryWrapper<EduMember>().eq(EduMember::getMobile, mobile));
        if(count>0){
            throw new JfException(20002,"注册失败-该手机号已注册请直接登录");
        }

        //判断图片验证码
        String imgStr = stringRedisTemplate.opsForValue().get(imgKey);

        if(StringUtils.isEmpty(imgStr) || !imgStr.equals(imgCode)){
            throw new JfException(20002,"注册失败-图形验证码输入错误或已超2分钟");

        }

        //判断短信验证码
        String redisSmsCode = stringRedisTemplate.opsForValue().get(mobile);
        if(StringUtils.isEmpty(redisSmsCode) || !redisSmsCode.equals(smsCode)){
            throw new JfException(20003,"注册失败-短信验证码输入错误或已超2分钟");

        }

        //数据添加数据库中
        EduMember member = new EduMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        byte[] salt = SaltPassUtils.generateSalt();
        String saltStr = Base64.getEncoder().encodeToString(salt);
        member.setSalt(saltStr);
        member.setPassword(SaltPassUtils.hashPassword(password,salt));//密码需要加盐加密的
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("https://lxledu.oss-cn-hangzhou.aliyuncs.com/avatar/default.jpg");//设置一个默认头像
        baseMapper.insert(member);

    }

    @Override
    public boolean saveOrUpdateMember(EduMember eduMemberVo) {
        if (eduMemberVo.getId() != null) { //修改
            //.trim()把空格去掉
            if (StringUtils.isEmpty(eduMemberVo.getMobile().trim())) {
                throw new RuntimeException("手机号必填");
            }
            if (StringUtils.isEmpty(eduMemberVo.getPassword())) {
                throw new RuntimeException("密码必填");
            }

            EduMember member = getById(eduMemberVo.getId());
            if(ObjectUtils.isEmpty(member)){
                throw new RuntimeException("账号不存在");
            }
            if(member.getIsDisabled()){
                throw new RuntimeException("账号已被禁用");
            }
            //给修改的密码加盐加密

            // 生成盐
            byte[] salt = SaltPassUtils.generateSalt();
            eduMemberVo.setSalt(Base64.getEncoder().encodeToString(salt));
            // 将密码和盐拼接在一起
            String hashedPassword = SaltPassUtils.hashPassword(eduMemberVo.getPassword(), salt);
            eduMemberVo.setPassword(hashedPassword);
            if (updateById(eduMemberVo)) {
                return true;
            }

        } else { //新增

        }
        return false;
    }

    @Override
    public boolean updateMember(EduMember eduMemberVo) {

        EduMember member = getById(eduMemberVo.getId());
        if(ObjectUtils.isEmpty(member)){
            throw new RuntimeException("账号不存在");
        }
        if(member.getIsDisabled()){
            throw new RuntimeException("账号已被禁用");
        }

        //校验密码是否正确（这里做了加盐加密）
        if (!SaltPassUtils.PasswordValidation(member.getSalt(),eduMemberVo.getPassword(),member.getPassword())) {
            return false;
        }
        return true;
    }
}
